package com.yonyou.pmclouds.messagenotice.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.messagenotice.entity.MessageNoticeVO;
import com.yonyou.pmclouds.messagenotice.rmiitf.MessageNoticeMaintain;
import com.yonyou.pmclouds.messagenotice.rmiitf.MessageNoticeQuery;
import lombok.Data;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

import static com.yonyou.pmclouds.basecom.PMControllerConst.DEFAULT_PAGE_PAGE_START;
import static com.yonyou.pmclouds.basecom.PMControllerConst.DEFAULT_PAGE_SIZE;

@RestController
@RequestMapping("/mobile/messageNotice")
public class MessageNoticeController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private MessageNoticeMaintain noticeMaintain;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private MessageNoticeQuery noticeQuery;

    @RequestMapping("/insert")
    @ResponseBody
    public MessageNoticeVO[] insertMessageNotice(@RequestBody MessageNoticeVO noticeVO) throws BusinessException{
        return noticeMaintain.insertMessageNotice(noticeVO);
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageObject<MessageNoticeVO> getNoticesByUser(@RequestParam("tenantId") String tenantId,
                                                        @RequestParam("userId") String userId,
                                                        @RequestParam(name = "pageStart", required = false,
                                                          defaultValue = DEFAULT_PAGE_PAGE_START) String pageStart,
                                                        @RequestParam(name = "pageSize", required = false,
                                                          defaultValue = DEFAULT_PAGE_SIZE) String pageSize) throws BusinessException{
        return noticeQuery.getNoticesByUser(tenantId, userId, Integer.valueOf(pageStart), Integer.valueOf(pageSize));
    }

    @RequestMapping("/delete")
    @ResponseBody
    public MessageNoticeVO[] deleteNotice(@RequestBody MessageNoticeVO[] noticeVOS) throws BusinessException{
        if(noticeVOS == null || noticeVOS.length <= 0) {
            return noticeVOS;
        }
        return noticeMaintain.deleteNotices(noticeVOS);
    }

    @RequestMapping("/read")
    @ResponseBody
    public Boolean noticeRead(@RequestBody ReadParam readParam) throws BusinessException{
        if(readParam == null || ArrayUtils.isEmpty(readParam.getNoticeIds())) return false;
        return noticeMaintain.updateNoticeRead(readParam.getNoticeIds());
    }


    @RequestMapping("/hasUnreadNotice")
    public Integer hasUnreadNotice() throws BusinessException {
        return noticeQuery.countUnreadNotice(RuntimeEnvironment.getLoginUserId());
    }

}


@Data
class ReadParam implements Serializable {

    private String[] noticeIds;
}